﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Net.Mail;
using SoftMedia.Opcion.Web.Models;

namespace SoftMedia.Opcion.Web.Controllers
{
    public class ContactenosController : Controller
    {
        //
        // GET: /Contactenos/

        public ActionResult Index()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Enviar(ContactenosModel model)
        {
            model.opcionInmueble = BindDDLItems("OpcionInmueble");
            model.opcionToyota = BindDDLItems("OpcionToyota");

            if (ModelState.IsValid)
            {
                string response;

                try
                {
                    Configuration config =
                        WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath + "/web.config");
                    string correoDestinatario = (string)config.AppSettings.Settings["CorreoDestinatario"].Value;
                    string correoRemitente = (string)config.AppSettings.Settings["CorreoRemitente"].Value;

                    MailMessage message = new MailMessage(correoRemitente, correoDestinatario,
                                                          "Contactenos:" + model.nombres, model.getMailBody());
                    SmtpClient emailClient = new SmtpClient("smtp.opcion.com.pe");
                    NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("postmaster@opcion.com.pe",
                                                                                      "46hrjj");
                    emailClient.UseDefaultCredentials = false;
                    emailClient.Credentials = SMTPUserInfo;
                    emailClient.Send(message);
                    response = "Mensaje enviado, nos contactaremos con ustedes a la brevedad posible";
                }
                catch (Exception ex)
                {
                    //ModelState.AddModelError("", "Ocurrio un error al momento de enviar los datos : " + ex.ToString());
                    response = "En este momento no podemos procesar su solicitud, intente mas tarde"; //+ ex.ToString();
                }
                //return View("Enviado", enviadoModel);
                ViewBag.SmtpResponse = response;

                return View("Enviado");
            }
            return View(model);
        }

        SelectList BindDDLItems(string ddlName)
        {

            List<SelectListItem> sliT = new List<SelectListItem>();

            switch (ddlName)
            {
                case "OpcionToyota":
                    sliT.Add(new SelectListItem() { Value = "yaris sedan", Text = "Yaris sedan      " });
                    sliT.Add(new SelectListItem() { Value = "yaris hb", Text = "Yaris hb            " });
                    sliT.Add(new SelectListItem() { Value = "corolla", Text = "Corolla              " });
                    sliT.Add(new SelectListItem() { Value = "auris", Text = "Auris                  " });
                    sliT.Add(new SelectListItem() { Value = "camry", Text = "Camry                  " });
                    sliT.Add(new SelectListItem() { Value = "avensis", Text = "Avensis              " });
                    sliT.Add(new SelectListItem() { Value = "prius", Text = "Prius                  " });
                    sliT.Add(new SelectListItem() { Value = "zellas", Text = "Zelas                 " });
                    sliT.Add(new SelectListItem() { Value = "hilux", Text = "Hilux 4x4              " });
                    sliT.Add(new SelectListItem() { Value = "urban-cruiser", Text = "Urban Cruiser  " });
                    sliT.Add(new SelectListItem() { Value = "rav4", Text = "RAV4                    " });
                    sliT.Add(new SelectListItem() { Value = "4runner", Text = "4Runner              " });
                    sliT.Add(new SelectListItem() { Value = "lc-prado", Text = "LC Prado            " });
                    sliT.Add(new SelectListItem() { Value = "fj-cruiser", Text = "FJ Cruiser        " });
                    sliT.Add(new SelectListItem() { Value = "fortuner", Text = "Fortuner            " });
                    sliT.Add(new SelectListItem() { Value = "hiace", Text = "Hiace                  " });
                    sliT.Add(new SelectListItem() { Value = "coaster", Text = "Coaster              " });
                    sliT.Add(new SelectListItem() { Value = "hino-duntro", Text = "Hino Duntro      " });
                    sliT.Add(new SelectListItem() { Value = "hino", Text = "Hino                    " });
                    break;
                case "OpcionInmueble":
                    sliT.Add(new SelectListItem() { Value = "comprar casa", Text = "comprar casa" });
                    sliT.Add(new SelectListItem() { Value = "comprar departamento", Text = "comprar departamento" });
                    sliT.Add(new SelectListItem() { Value = "comprar terreno", Text = "comprar terreno" });
                    sliT.Add(new SelectListItem() { Value = "comprar local", Text = "comprar local" });
                    sliT.Add(new SelectListItem() { Value = "construir", Text = "construir" });
                    sliT.Add(new SelectListItem() { Value = "remodelar o ampliar", Text = "remodelar o ampliar" });
                    break;
            }

            return new SelectList(sliT, "Value", "Text");
        }

        public ActionResult Enviar()
        {
            var model = new ContactenosModel()
                            {
                                opcionInmueble = BindDDLItems("OpcionInmueble"),
                                opcionToyota = BindDDLItems("OpcionToyota")
                            };


            return View(model);
        }

        public ActionResult Enviado()
        {


            return View();
        }

    }
}
